package sim1011.behaviours;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import sim1011.starter.Logger;

public class PowerVampireBehaviour extends TickerBehaviour {
	private static final long serialVersionUID = 6093237980503467266L;
	private AID[] mobileResourceManager;

	public PowerVampireBehaviour(Agent a, long period) {
		super(a, period);
	}

	@Override
	protected void onTick() {
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("ecto-resource");
		sd.setName("ecto-resource.Node.M");
		template.addServices(sd);
		try {
			DFAgentDescription[] result = DFService.search(myAgent, template);
			mobileResourceManager = new AID[result.length];
			for (int i = 0; i < result.length; ++i) { // dovrebbe essere uno
														// solo nel sistema
				mobileResourceManager[i] = result[i].getName();
			}
			if (result.length > 0) {
				ACLMessage msg = new ACLMessage(ACLMessage.REQUEST_WHEN);
				msg.setContent("decrease battery");
				msg.addReceiver(mobileResourceManager[0]);
				myAgent.send(msg);
				Logger.log("[POWERVAMPIRE] Decrease battery message sent");
			}
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
	}
}